﻿package eu.liquify.ui {
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Graphics;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BevelFilter;
	import flash.filters.GlowFilter;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.text.AntiAliasType;
	import flash.text.GridFitType;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	public class Label extends UIItem {
		protected var txt:TextField = new TextField();
		protected var ellipsisFormat:TextFormat = new TextFormat(null, null, 0x727272);
		
		public var centerX:Boolean = false;
		public var centerY:Boolean = false;
		
		public var padding:Number = 3;
		
		public function Label(options:Object = null) {
			super();
			
			fixedSize = false;
			super.label = "Label";
			
			applyOptions(options);
			
			//debugSize = true;
			
			txt.defaultTextFormat = new TextFormat("Tahoma", 9, 0x000000);
			//txt.defaultTextFormat = new TextFormat("LabelFont", 10, 0x000000);
			//txt.defaultTextFormat = new TextFormat("LabelFont", 12, 0x000000);
			//txt.defaultTextFormat = new TextFormat("LabelFont", 16, 0xFFFFFF);
			//txt.embedFonts = true;
			//txt.gridFitType = GridFitType.PIXEL;
			txt.antiAliasType = AntiAliasType.ADVANCED;
			//txt.antiAliasType = AntiAliasType.;
			//txt.sharpness = 200;
			txt.selectable = txt.tabEnabled = txt.mouseEnabled = txt.mouseWheelEnabled = false;
			//txt.border = true;
			addChild(txt);
			
			tabEnabled = mouseEnabled = false;
			
			if (tabIndex == -1) tabIndex = nextTabIndex++;
			
			resize();
		}
		
		override public function set label(value:String):void {
			super.label = value;
			resize();
		}
		
		override public function resize():void {
			super.resize();
			txt.text = label ? label : "";
			
			txt.x = padding;
			txt.y = padding;
			
			if (fixedWidth || fixedHeight) {
				var txtWidth:Number;
				var txtHeight:Number;
				if (!fixedWidth || !fixedHeight || centerX || centerY) {
					txt.autoSize = TextFieldAutoSize.LEFT;
					txtWidth = txt.width;
					txtHeight = txt.height;
					txt.autoSize = TextFieldAutoSize.NONE;
				}
				if (fixedWidth) {
					Utils.resizeTextField(txt, label, width-padding*2, ellipsisFormat);
					if (centerX) {
						txt.x = width/2-txtWidth/2;
					}
				} else {
					txt.width = txtWidth;
					width = txtWidth+padding*2;
				}
				if (fixedHeight) {
					var h:Number = height-padding*2;
					txt.height = txtHeight < h ? txtHeight : h;
					if (centerY) {
						txt.y = height/2-txtHeight/2;
					}
				} else {
					txt.height = txtHeight;
					height = txtHeight+padding*2;
				}
			} else {
				txt.autoSize = TextFieldAutoSize.LEFT;
				
				width = padding*2+txt.width;
				height = padding*2+txt.height;
			}
		}
	}
	
}